<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 朋友圈模型
 *
 * @author khj
 */
class MerchantFriendsCircleModel extends MY_Model{

	public $tbFriends = 'merchant_friends_circle';
    public $tbUser = 'merchant';
    public $tbShop = 'merchant_shop';

    /**
     * 获取列表数据
     * @return 	array
     * @author khj
     */
    public function adminPcGetList(){
        $merchant = trim(get('merchant')); // 所属用户
        $content = trim(get('m_content')); // 内容
        $address = trim(get('m_address')); // 地址
        $status = (int)get('m_status'); // 状态 1 隐藏 2 显示

        $star = strtotime(get('m_send_date_from'));
        $end = strtotime(get('m_send_date_to'));
        $iSortCol_0 = (int)get('iSortCol_0');//针对某一列进行排序
        $sSortDir_0 = trim(get('sSortDir_0'));//排序方式，值为"asc"或者"desc"

        if($merchant){
            $this->db->like(array('s.shop_name' => $merchant));
        }
        if($content){
            $this->db->like(array('f.content' => $content));
        }
        if($address){
            $this->db->like(array('f.address' => $address));
        }
        if($status){
            $this->db->where(array('f.status' => $status));
        }
        if($star){
            $this->db->where('f.addtime >=', $star);
        }
        if($end){
            $this->db->where('f.addtime <= ', strtotime('+1day', $end));
        }

        if ($iSortCol_0 == 4){
            $this->db->order_by('f.browse_number', $sSortDir_0);
        }else if ($iSortCol_0 == 6){
            $this->db->order_by('f.addtime', $sSortDir_0);
        }else{
            $this->db->order_by('f.addtime', 'desc');
        }

        $this->db->select('f.id,f.content,f.latitude,f.longitude,f.address,f.browse_number,f.status,f.addtime,
        s.shop_name as merchant')
            ->join($this->tbUser.' m', 'f.merchant_id=m.id')
            ->join($this->tbShop.' s', 's.merchant_id=m.id')
            ->where('is_delete is null');
        $data = $this->pagination($this->tbFriends.' f');
        return  $data;
    }

	/**
	 * 保存信息
	 * @param 	array 	$data
	 * @param   integer $id
	 * @param   integer $videoId  视频id
	 * @return	boolean|integer
	 * @author khj
	 */
	public function save($data, $id = 0, $videoId = ''){
	    if($id){
	        $this->db->where('id', $id);
	        return $this->update($this->tbFriends, $data);
	    }elseif ($videoId){
            $this->db->where('video', $videoId);
            return $this->update($this->tbFriends, $data);
        }
	
	    return $this->insert($this->tbFriends, $data);
	}

    /**
     * 获取状态
     *
     * @param	integer	$key
     * @param	boolean	$show	是否显示
     * @return	mixed
     */
    public function getStatus($key = null, $show = false){

        $config = array(
            1 => '隐藏',
            2 => '显示',
        );

        if($key !== null){

            if(isset($config[$key]) && ! $show){
                return $config[$key];
            }

            if(isset($config[$key]) && $show){

                if($key == 1){
                    return '<span class="label label-danger">'.$config[$key].'</span>';
                }else if($key == 2){
                    return '<span class="label label-success">'.$config[$key].'</span>';
                }
            }
            return '';
        }

        return $config;
    }


    /**
     * 删除我的动态
     * @param string $fid  动态id
     * @param int $merchantId  发布者id
     * @return bool
     */
    public function deleteCircle($fid, $merchantId){
        if(!$fid || !$merchantId)return false;
        $this->db->where(array('id' => $fid,'merchant_id' => $merchantId));

        return $this->delete($this->tbFriends);
    }


}